﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//对局UI展示（中央文字提示）
public class VSUI : MonoBehaviour
{
    private static VSUI m_Instance = null;
    public List<Transform> Cards = new List<Transform>();
    public float SubjectYDes = -4.19f;
    public float SubjectYSrc = -6.5f;
    public float ObjectYSrc;
    public float ObjectYDes;
    public float ShowCardTime = 0.5f;
    public float ArrangeCardTime = 0.5f;
    //最多显示的不覆盖的卡牌数量
    public int ShowAllCardMaxCount = 5;
    //正常卡牌间隔（不覆盖的情况下）
    public float NormalXSpacing = 1.94f;
    public static VSUI Instance {
        get {
            if(m_Instance == null) {
                m_Instance = GameObject.FindObjectOfType<VSUI>() as VSUI;
            }
            if(!m_Instance) {
                GameObject obj = new GameObject("VSUI");
                m_Instance = obj.AddComponent<VSUI>();
            }
            return m_Instance;
        }
    }

    // Start is called before the first frame update
    public void GameStart() {
        
    }
    public void Draw(CardProps.ID id, int handIndex) {
        GameObject newCard = CardFactory.Instance.GetNormalCard(id, handIndex);
        Cards.Add(newCard.transform);
        //newCard.GetComponent<SpriteRenderer>().sortingOrder = -Cards.Count;
        List<float> desPosX = CalculateDesPosX(Cards.Count);
        newCard.transform.position = new Vector3(desPosX[Cards.Count - 1], SubjectYSrc, 0f);
        if(Cards.Count >= 2)
            newCard.transform.position += Vector3.forward *(Cards[Cards.Count - 2].transform.position.z + 1);
        ArrangeHandCard(desPosX, 0, Cards.Count - 1);
        StartCoroutine(ShowCard(newCard));
    }
    public void DestroyCard(int index) {
        GameObject obj = Cards[index].gameObject;
        Cards.RemoveAt(index);
        CardFactory.Instance.ReturnNormalCard(obj);
        List<float> desPosX = CalculateDesPosX(Cards.Count);
        //Debug.Log(desPosX);
        ArrangeHandCard(desPosX, 0, Cards.Count);
        for(int i = 0; i < Cards.Count; ++i) {
            Cards[i].GetComponent<CardUI>().HandIndex = i;
        }
    }
    public void ClearHandCard() {
        for(int i = 0; i < Cards.Count; ++i) {
            GameObject obj = Cards[i].gameObject;
            CardFactory.Instance.ReturnNormalCard(obj);
        }
        Cards.Clear();
    }
    
    private IEnumerator ShowCard(GameObject card) {
        card.SetActive(true);
        yield return StartCoroutine(MoveCardY(card.transform, 0.5f, SubjectYSrc, SubjectYDes));
        card.GetComponent<CardUI>().DragActive();
    }
    private void ArrangeHandCard(List<float> desPosX, int begin, int end) {
        for(int i = begin; i < end; ++i) {
            StartCoroutine(MoveCardX(Cards[i], ArrangeCardTime, desPosX[i]));
        }
    }
    
    public void SwitchSide() {

    }
    public void EffectTrigger() {

    }
    //返回整理后卡牌位置，从右到左为0，1，2...
    private List<float> CalculateDesPosX(int count) {
        List<float> posList = new List<float>();
        if(count <= ShowAllCardMaxCount) {
            float cardTotalLength = NormalXSpacing * (count - 1);
            float firstCardPosX = cardTotalLength / 2;
            for(int i = 0; i < count; ++i) {
                posList.Add(firstCardPosX - i * NormalXSpacing);
            }
        } else {
            float XHalfThreshold = NormalXSpacing  * (ShowAllCardMaxCount - 1) / 2;
            float newSpacing = XHalfThreshold * 2 / (count - 1);
            float firstCardPosX = XHalfThreshold;
            for(int i = 0; i < count; ++i) {
                posList.Add(firstCardPosX - i * newSpacing);
            }
        }
        return posList;
    }
    IEnumerator MoveCardY(Transform card, float time, float srcY, float desY) {
        if(time < 0) time = Mathf.Abs(time);
        float percent = 0;
        while(percent < 1f) {
            float currY = Mathf.SmoothStep(srcY, desY, percent);
            card.position = new Vector3(card.position.x, currY, card.position.z);
            percent += Time.deltaTime / time;
            yield return null;
        }
    }
    IEnumerator MoveCardX(Transform objTran, float time, float desX) {
        float srcX = objTran.position.x;
        if(time < 0) time = Mathf.Abs(time);
        float percent = 0;
        while(percent < 1f) {
            float currX = Mathf.SmoothStep(srcX, desX, percent);
            objTran.position = new Vector3(currX, objTran.position.y, objTran.position.z);
            percent += Time.deltaTime / time;
            yield return null;
        }
    }
}
